<template>
  <base-view :model="query" @submit="search">
    <template v-slot:params>
      <a-col :span="8">
        <a-form-model-item label="渠道标识">
          <a-input
            v-model="query.srcChannelId"
            placeholder="输入渠道标识"
            allow-clear
          />
        </a-form-model-item>
      </a-col>
      <a-col :span="8">
        <a-form-model-item label="渠道名称">
          <a-input
            placeholder="输入渠道名称"
            v-model="query.srcChannelName"
            allow-clear
          />
        </a-form-model-item>
      </a-col>
      <a-col :span="24">
        <a-form-model-item class="search">
          <a-button type="primary" html-type="submit">搜索</a-button>
          <a-button style="margin-left: 8px" html-type="reset" @click="reset">重置</a-button>
        </a-form-model-item>
      </a-col>
    </template>
    <div class="section">
      <a-table
        ref="table"
        :columns="columns"
        :dataSource="dataSource"
        :rowKey="(record,index)=>{return index}"
        :loading="loading"
        :pagination="pagination"
        :scroll="{ x: 1650 }"
        @change="pageChange"
        @showSizeChange="pageChange"
      >
        <template slot="operation" slot-scope="text, record">
          <a-icon type="setting" theme="twoTone" twoToneColor="#4a9ff5" @click="edit(record)" title="修改" v-has-permission="['pSrcChannel:update']"></a-icon>
          <a-icon type="eye" theme="twoTone" twoToneColor="#4a9ff5" @click="view(record)" title="详情" v-has-permission="['pSrcChannel:view']"></a-icon>
          <a-icon type="check-circle" v-hasPermission="['pSrcChannel:update']" v-if="record.transChannelStatus !== 'Y'" @click="stop(record)" theme="twoTone" twoToneColor="#4a9ff5" title="启用"></a-icon>
          <a-icon type="minus-circle" v-if="record.transChannelStatus !== 'N'" v-hasPermission="['pSrcChannel:update']" @click="stop(record)" theme="twoTone" twoToneColor="#4a9ff5" title="停用"></a-icon>

          <a-badge
            status="warning"
            text="无权限"
            v-has-no-permission="['pSrcChannel:view', 'pSrcChannel:update']"
          ></a-badge>
        </template>
      </a-table>
    </div>
    <a-modal
      v-model="modalVisible"
      :title="modalTitle"
      width="1050px"
      :scroll="{ x: 1500 }"
      @ok="submit"
      :maskClosable="false"
      :footer="isHideFoot?null:undefined"
    >
      <p-src-channel-operation ref="modalForm" :query="query" @closeModal="closeModal" />
    </a-modal>
  </base-view>
</template>

<script>
  import PSrcChannelOperation from "./PSrcChannelOperation";

  export default {
    name: "PSrcChannel",
    components: { PSrcChannelOperation },
    data: function () {
      return {
        beanInfo: {},
        modalVisible: false,
        modalTitle:"",
        operationType: "view",
        query: {instId:'', srcChannelId: "", srcChannelName: "" },
        loading: false,
        pagination: {
          pageNum: 1,
          pageSize: 10,
          pageSizeOptions: ["10", "20", "30", "40", "100"],
          defaultCurrent: 1,
          defaultPageSize: 10,
          showQuickJumper: true,
          showSizeChanger: true,
          showTotal: (total, range) =>
            `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`,
        },
        columns: [
          {
            title: "机构标识",
            dataIndex: "instId",
            fixed: "left",
            width: 120,
          },
          {
            title: "渠道标识",
            dataIndex: "srcChannelId",
            width: 100,
          },
          {
            title: "渠道名称",
            dataIndex: "srcChannelName",
            width: 200,
          },
          {
            title: "业务渠道",
            dataIndex: "transChannelId",
            width: 120,
          },
          {
            title: "业务渠道名称",
            dataIndex: "transChannelName",
            width: 120,
          },
          {
            title: "渠道状态",
            dataIndex: "transChannelStatus",
            width: 120,
            customRender: (text, row, index) => {
              switch (text) {
                case "Y":
                  return "启用";
                case "N":
                  return "停用";
                default:
                  return text;
              }
            },
          },
          {
            title: "业务控制规则",
            dataIndex: "bizCtrlRule",
            width: 150,
            customRender: (text, row, index) => {
              switch (text) {
                case "0":
                  return "按自然日";
                case "1":
                  return "按工作日";
              }
            },
          },
          {
            title: "营业开始日期",
            dataIndex: "bizStartDate",
            width: 150,
          },
          {
            title: "营业截止时间",
            dataIndex: "bizEndTime",
            width: 150,
          },
          {
            title: "系统安全计划",
            dataIndex: "secPlanId",
            width: 150,
          },
          {
            title: "操作",
            fixed: "right",
            scopedSlots: { customRender: "operation" },
          },
        ],
        dataSource: [],
      };
    },
    methods: {
      async submit() {
        const res = await this.$refs.modalForm.submit();
        if (res) {
          this.modalVisible = false;
          this.pagination.defaultCurrent = 1;
          await this.search();
        }
      },
      async reset(){
        this.query = {instId:this.query.instId}
        this.pagination.pageNum = this.pagination.defaultCurrent;
        this.pagination.pageSize = this.pagination.defaultPageSize;
        await this.search();
      },
      async search(e) {
        e && e.preventDefault && e.preventDefault();
        console.log(e);
        try {
          const res = await this.$get("/admin/igas/pSrcChannelInfo", {
            instId: this.query.instId,
            srcChannelId: this.query.srcChannelId,
            srcChannelName: this.query.srcChannelName,
            pageNum: this.pagination.pageNum,
            pageSize: this.pagination.pageSize,
          });
          this.dataSource = res.data.rows;
          this.$set(this.pagination, "total", +res.data.total);
        } catch (e) {
          console.error(e);
        }
      },
      openModal(type = "add", beanInfo) {
        this.modalTitle = {
          add: "新增",
          edit: "编辑",
          view: "详情",
          cancel: beanInfo.transChannelStatus === "Y" ? "启用" : "停用",
        }[type];
        this.modalVisible = true;
        if(type === 'view'){
          this.isHideFoot = true;
        }else{
          this.isHideFoot = false
        }
        this.$nextTick(() => {
          this.$refs.modalForm.setOperatorType(type);
          if (type === "edit" || type === "view" || type === "cancel") {
            this.$refs.modalForm.setBeanInfo(beanInfo);
          }
          this.$refs.modalForm.resetFields();
        });
      },
      edit(item) {
        if(item.transChannelStatus==='N'){
          return  this.$message.error('已停用数据不能再修改！');
        }
        this.openModal("edit", item);
      },
      view(item) {
        this.openModal("view", item);
      },

      async stop(record) {
        const res = await this.$put(
          "/admin/igas/pSrcChannelInfo/transChannelStatus",
          record
        );
        if (res.status == 200) {
          this.$message.success("保存成功！");
          this.search();
          return true;
        } else {
          this.$message.error(res.data.message);
          return false;
        }
      },
      closeModal(){
        this.modalVisible = false
      },
      pageChange(e) {
        this.pagination.pageNum = e.current;
        this.pagination.pageSize = e.pageSize;
        this.search();
        this.pagination.pageNum = this.defaultCurrent
      },
    },
  };
</script>
<style lang="less" scoped>
  @import "../../../../../static/less/Common";
</style>
